package com.smart.jetpackcomposestate.examples

import android.os.Bundle
import android.os.Parcelable
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.listSaver
import androidx.compose.runtime.saveable.mapSaver
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.smart.jetpackcomposestate.ui.theme.DN_Compose_LearnTheme
import kotlinx.parcelize.Parcelize

/**
 * @author huangzehui
 * @date 16/8/2025 上午12:22 周六
 * @description
 *
 * 40 - 状态恢复
 * https://www.bilibili.com/video/BV1ob4y1a7ad?spm_id_from=333.788.player.switch&vd_source=e07cc8cc94eb22bd76722071b03cd199&p=40
 */
class StateRecoveryParcelableActivity : ComponentActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            DN_Compose_LearnTheme {
                CityScreen()
            }
        }
    }


    @Composable
    private fun CityScreen() {
        // rememberSaveable 在重新创建activity或进程后，我们可以使用rememberSaveable恢复界面状
        //态。rememberSaveable可以在重组后保持状态。此外，rememberSaveable
        //也可以在重新创建activity和进程后保持状态。

        // 旋转屏幕保存数据 Saveable 系列的 第一种写法
        // rememberSaveable 的数据，屏幕旋转后，数据依旧会保留
//        val (city, setCity) = rememberSaveable {
//            mutableStateOf(City(name = "Madrid", country = "Spain"))
//        }
        // 使用 remember，旋转屏幕后，不会保存数据，会重新使用初始值
//        val (city, setCity) = remember {
//            mutableStateOf(City(name = "Madrid", country = "Spain"))
//        }

        // 旋转屏幕保存数据 Saveable 系列的 第二种写法 mapSaver
        // 指定mapSaver，用于存储时如何构建一个map对象，获取时如何构建一个City对象
//        val citySaver = kotlin.run {
//            val nameKey = "Name"
//            val countryKey = "Country"
//
//            mapSaver<City>(
//                save = { mapOf(nameKey to it.name, countryKey to it.country) },
//                restore = { City(it[nameKey] as String, it[countryKey] as String) }
//            )
//        }
//
//        val (city, setCity) = rememberSaveable(stateSaver = citySaver) {
//            mutableStateOf(City("Madrid", "Spain"))
//        }


        // 旋转屏幕保存数据 Saveable 系列的 第三种写法
        val citySaver = listSaver<City, Any>(
            save = { listOf(it.name, it.country) },
            restore = { City(it[0] as String, it[1] as String) }
        )

        val (city, setCity) = rememberSaveable(stateSaver = citySaver) {
            mutableStateOf(City("Madrid", "Spain"))
        }

        Row(
            verticalAlignment = Alignment.CenterVertically,
            modifier = Modifier.padding(10.dp)
        ) {
            // 点击按钮修改 City 信息
            TextButton(
                colors = ButtonDefaults.buttonColors(),
                onClick = {
                    setCity(City("Beijing", "China"))

                }) {
                Text(text = "Click to change")
            }

            Text(text = "${city.country}-${city.name}")
        }
    }


    /**
     * 使用   @Parcelize 就可以方便的进行 Parcelable 需要复写的方法了
     */
    @Parcelize
    data class City(val name: String, var country: String) : Parcelable
}